package com.mall.commons.utils;

import lombok.Data;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {

    private Integer code; //编码：1成功，0和其它数字为失败

    private String message; //错误信息

    private T data; //数据

    /**
     * 成功返回结果
     * @param object
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T object,String message) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.data = object;
        commonResult.code = 200;
        commonResult.message = message;
        return commonResult;
    }
    /**
     * 成功返回结果
     * @param object
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> success(T object) {
        CommonResult<T> commonResult = new CommonResult<T>();
        commonResult.data = object;
        commonResult.code = 200;
        commonResult.message = "操作成功";
        return commonResult;
    }

    /**
     * 失败返回结果
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.message = msg;
        commonResult.code = 0;
        return commonResult;
    }

    /**
     * 失败返回结果
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> error(Integer code,String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.message = msg;
        commonResult.code = code;
        return commonResult;
    }

}
